import React from 'react';
import { Modal } from 'antd';
import { useRequest, defaultFormatResult } from '@/hooks';
import pageTabUtil from '@/utils/pageTabUtil';

import { getUnSubmitInterviewProcedure } from '../../service';
import { AbsentTable } from '../containers';

const TabModal = React.memo(({ visible, setVisible, disabled, ...rest }) => (
    <Modal
        okButtonProps={{
            disabled
        }}
        title="提醒"
        visible={visible}
        bodyStyle={{ padding: 0 }}
        okText="面谈总结"
        onOk={() => {
            setVisible(false);
            pageTabUtil.refreshTab();
        }}
        onCancel={() => {
            // setVisible(false);
            pageTabUtil.goBack();
        }}
    >
        <AbsentTable {...rest} />
    </Modal>
));

export default (arrangeId, isConclusion) => {
    const [visible, setVisible] = React.useState(false);
    const [disabled, setDisabled] = React.useState(true);

    // 接口在modal内又查了一遍，似乎没有更好的方法
    const { data: dataSource } = useRequest(() => getUnSubmitInterviewProcedure(arrangeId), {
        manual: !isConclusion,
        ...defaultFormatResult
    });

    const modalProps = {
        visible,
        setVisible,
        disabled,
        setDisabled,
        arrangeId
    };

    // 是否显示弹窗
    React.useEffect(() => {
        if (isConclusion && dataSource?.length && dataSource?.some(item => !item.absentInterviewFlag)) {
            setVisible(true);
        }
    }, [dataSource, isConclusion, setVisible]);

    const tabModalContent = <TabModal {...modalProps} />;

    return { tabModalContent };
};
